package com.markbuikema.juliana32.util.facebook;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.markbuikema.juliana32.model.nieuws.Comment;
import com.markbuikema.juliana32.model.nieuws.FacebookNieuwsItem;
import com.markbuikema.juliana32.model.nieuws.Like;
import com.markbuikema.juliana32.model.nieuws.NieuwsItem;
import com.markbuikema.juliana32.model.teams.FacebookEvent;
import com.markbuikema.juliana32.util.DateTimeUtils;

public class FacebookParser {

	public static List<FacebookNieuwsItem> parseFeed(JSONObject json) {
		List<FacebookNieuwsItem> stories = new ArrayList<FacebookNieuwsItem>();
		try {

			try {
				JSONArray array = json.getJSONArray("data");
				for (int i = 0; i < array.length(); i++) {
					FacebookNieuwsItem item = processJSONObject(array.getJSONObject(i));
					if (item != null)
						stories.add(item);
				}
			} catch (JSONException e) {
				FacebookNieuwsItem item = processJSONObject(json.getJSONObject("data"));
				if (item != null)
					stories.add(item);
			}
		} catch (JSONException e) {
		}

		int count = 0;
		for (NieuwsItem i : stories)
			if (i instanceof FacebookNieuwsItem)
				count++;

		return stories;

	}

	private static FacebookNieuwsItem processJSONObject(JSONObject o) {
		boolean photo = false;
		try {
			if (o.getString("type").equals("photo"))
				// Log.d("PHOTOJSON", o.toString());
				photo = true;
		} catch (JSONException e) {
		}

		String id;
		String title;
		String content;
		String dateString;
		String link;
		String imgUrl;
		List<Like> likes = new ArrayList<Like>();
		List<Comment> comments = new ArrayList<Comment>();
		String albumId = null;
		String defaultPhoto;
		if (photo)
			try {
				String parsableAlbum = o.getString("link");
				// Log.d("ALBUM", "1: " + parsableAlbum);
				parsableAlbum = parsableAlbum.split("photos/a\\.")[1];
				// Log.d("ALBUM", "2: " + parsableAlbum);
				albumId = parsableAlbum.split("\\.")[0];
				// Log.d("ALBUM", "3: " + albumId);

			} catch (JSONException e1) {
				e1.printStackTrace();
			} catch (IndexOutOfBoundsException e) {
				e.printStackTrace();
			}

		if (albumId != null && albumId.equals(FacebookManager.FACEBOOK_TIMELINE_PHOTOS_ALBUM_ID))
			albumId = null;

		try {
			if (!o.getJSONObject("from").getString("name").toLowerCase(Locale.US).contains("juliana"))
				return null;
		} catch (JSONException e) {
			return null;
		}

		try {
			defaultPhoto = o.getString("object_id");
		} catch (JSONException e) {
			defaultPhoto = null;
		}

		try {
			id = o.getString("id");
		} catch (JSONException e) {
			id = null;
		}
		// Log.d( "FB_POST", "pre message parse" );
		try {
			content = o.getString("message");
		} catch (JSONException e) {
			content = null;
		}
		// Log.d( "FB_POST", "post message parse" );

		try {
			if (photo)
				title = o.getString("name");
			else
				title = "Facebook";
		} catch (JSONException e) {
			title = "Facebook";
		}

		try {
			dateString = o.getString("created_time");
		} catch (JSONException e) {
			dateString = null;
		}

		try {
			link = o.getString("link");
		} catch (JSONException e) {
			link = null;
		}

		try {
			imgUrl = o.getString("picture");
		} catch (JSONException e) {
			imgUrl = null;
		}

		try {
			JSONObject likeJSON = o.getJSONObject("likes");
			JSONArray data = likeJSON.getJSONArray("data");
			for (int i = 0; i < data.length(); i++) {
				JSONObject like = data.getJSONObject(i);
				String name = like.getString("name");
				String likeId = like.getString("id");
				likes.add(new Like(likeId, name));
			}
		} catch (JSONException e) {
		}

		try {
			JSONObject commentJSON = o.getJSONObject("comments");
			JSONArray commentArray = commentJSON.getJSONArray("data");
			for (int i = 0; i < commentArray.length(); i++) {
				JSONObject c = commentArray.getJSONObject(i);
				Comment comment = new Comment(c.getString("id"), c.getJSONObject("from").getString("name"), c.getJSONObject("from").getString("id"), c.getString("message"),
						c.getString("created_time"));
				comments.add(comment);
			}
		} catch (JSONException e) {
		}

		GregorianCalendar date = DateTimeUtils.fbStringToDate(dateString);

		if (!photo && content == null)
			return null;

		FacebookNieuwsItem item = new FacebookNieuwsItem(id, title, content, date, link, imgUrl, likes, comments, photo ? albumId : null, photo ? defaultPhoto : null);

		return item;

	}

	public static List<FacebookEvent> parseEvents(JSONObject json) {
		List<FacebookEvent> events = new ArrayList<FacebookEvent>();
		try {
			JSONArray data = json.getJSONArray("data");
			for (int e = 0; e < data.length(); e++) {
				JSONObject ev = data.getJSONObject(e);
				String id = ev.getString("id");
				Log.d("dateparser", ev.toString());
				long startTime = DateTimeUtils.fbStringToDate(ev.optString("start_time")).getTimeInMillis();
				GregorianCalendar end = DateTimeUtils.fbStringToDate(ev.optString("end_time"));
				long endTime = -1;
				if (end != null) {
					endTime = end.getTimeInMillis();
				}
				String title = ev.getString("name");
				String location = ev.getString("location");

				events.add(new FacebookEvent(id, startTime, location, endTime, title));

			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return events;
	}
}
